﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admins_Order_Yeu_Cau : System.Web.UI.Page
{
     public static int Id;
    protected static int tong_tien;
    protected void Page_Load(object sender, EventArgs e)
    {
        grid_order_yeu_cau.ItemCommand += new DataGridCommandEventHandler(grid_order_yeu_cau_ItemCommand);
        grid_order_yeu_cau1.ItemCommand += new DataGridCommandEventHandler(grid_order_yeu_cau_ItemCommand);
        grid_order_yeu_cau.PageIndexChanged += new DataGridPageChangedEventHandler(grid_order_yeu_cau_PageIndexChanged);
        if (!IsPostBack)
        {
            layOrderyeucau();
        }
    }

    public void layOrderyeucau()
    {
        TourYeuCau orderyeucau = new TourYeuCau();
        List<ObjTourYeuCau> lstOrderyeucau = new List<ObjTourYeuCau>();
        lstOrderyeucau = orderyeucau.getOrderYeuCauByStatus(Constants.STATUS_ALL);
        if (lstOrderyeucau.Count > 0)
        {
            grid_order_yeu_cau.DataSource = lstOrderyeucau;
            grid_order_yeu_cau.DataBind();
        }
        else
        {
            lb_messenger.Text = "Chưa có yêu cầu";
        }
        pnView.Visible = true;
        pnUpdate.Visible = false;
    }
    void grid_order_yeu_cau_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grid_order_yeu_cau.CurrentPageIndex = e.NewPageIndex;
        layOrderyeucau();
    }

    

    void grid_order_yeu_cau_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string strCA = e.CommandArgument.ToString();
        Id = int.Parse(strCA);
        switch (e.CommandName)
        {
            case "Edit":
                pnUpdate.Visible = true;
                pnView.Visible = false;
               TourYeuCau orderyeucau = new TourYeuCau();
                List<ObjTourYeuCau> lstOrderyeucau = new List<ObjTourYeuCau>();
                lstOrderyeucau = orderyeucau.getTourYeuCauById(Id);
                if (lstOrderyeucau.Count > 0)
                {
                    grid_order_yeu_cau1.DataSource = lstOrderyeucau;
                    grid_order_yeu_cau1.DataBind();
                }
                ck_lien_he.Checked = true;
                if (e.Item.Cells[2].Text == "1")
                {
                    ck_lien_he.Checked = false;
                }
                break;

            case "Delete":
                   TourYeuCau touryeucau = new TourYeuCau();
                touryeucau.TourYeuCauUpdateStatus(0, Id);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                break;

        }
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
      

    }
    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        layOrderyeucau(); 
    }
    protected void Update_Click(object sender, EventArgs e)
    {
        if(Id!=0)
        {
            TourYeuCau touryeucau = new TourYeuCau();
            int status = 1;
            if (ck_lien_he.Checked == true)
            {
                status = 2;
            }
            touryeucau.TourYeuCauUpdateStatus(status, Id);
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }
    }
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        string xml = "";
        
        foreach (DataGridItem item in grid_order_yeu_cau.Items)
        {
            CheckBox checkBox = (CheckBox)item.Cells[0].Controls[1];

            if (checkBox.Checked == true)
            {
                xml += "<TourYeuCau Order_yeu_cau_id=\"" + item.Cells[1].Text + "\" ></TourYeuCau>";
            }
        }
        if (xml != "")
        {
            TourYeuCau touryeucau = new TourYeuCau();
            touryeucau.TourYeuCauDeleteByXml(xml);
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }
    }

    protected void btn_tim_kiem_Click(object sender, EventArgs e)
    {
        TourYeuCau touryeucau = new TourYeuCau();
        List<ObjTourYeuCau> lst = touryeucau.TourYeuCauSearch(txt_tim_kiem.Text);
        if (lst.Count > 0)
        {
            grid_order_yeu_cau.DataSource = lst;
            grid_order_yeu_cau.DataBind();
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        pnView.Visible = true;
        pnUpdate.Visible = false;
    }
}